<?php
/**
 * Created by PhpStorm.
 * User: 刘玉
 * Date: 2020/3/23
 * Time: 5:00
 */

namespace app\api\controller\v15;

use app\aliyun\controller\AliyunFilterWord;
use app\api\controller\v2\Auth;
use app\common\cachemodel\ZlUserVip;
use app\common\model\Article as ArticleModel;
use app\common\model\ArticleComment;
use app\common\model\ArticleCommentReply;
use app\common\model\MessageCenter;
use think\Cache;
use think\response\Json;

/**
 * @title 文章相关接口
 * @desc 查看文章详情<br/>按页码获取文章列表，每页十条<br/>
 * @version 1.10.2
 * Class Article
 * @package app\api\controller\v2
 */
class Article extends Auth
{
    /**
     * 文章评论
     * @return Json
     * @throws \Exception
     */
    public function comment()
    {
        $id = input('param.id/d');
        $content = htmlspecialchars(input('param.content/s', ''));
        $content = trim($content);
        if (empty($content))
        {
            return show(config('code.error'), '评论内容不能为空');
        }
        if ($this->user['no_comment'])
        {
            return show(config('code.error'), '评论失败，已被禁止评论');
        }
        //获取文章详情
        $article = (new ArticleModel())->getArticleDetails($this->user['id'], ['id' => $id]);
        if (!$article)
        {
            return show(config('code.error'), '文章不存在');
        }
        $res = AliyunFilterWord::checkText($content, 'article_comment');
        //若内容有违规且超过配置的相似比例，则进行隐藏不显示，将标签存入库中
        if ($res)
        {
            $type = 1;
            $data = [
                'check_status' => 1,
                'illegal_type' => $res['label_type'],
                'illegal_level' => $res['level'],
                'data_id' => $res['data_id'],
                'aid' => $id,
                'content' => $content,
                'ucid' => $this->user['uc_id'],
                'uid' => $this->user['id'],
                'status' => -1
            ];
        } else
        {
            $type = 2;
            $data = [
                'aid' => $id,
                'content' => $content,
                'ucid' => $this->user['uc_id'],
                'uid' => $this->user['id'],
                'status' => 0
            ];
        }
        $rec = (new ArticleComment())->add_comment($this->user, $data);
        if ($rec)
        {
            if ($type === 1)
            {
                $messageData = [
                    'status' => 0,
                    'is_read' => 0,
                    'range' => $this->user['id'],
                    'type' => 1,
                    'article_comment_id' => $rec['cid'],
                    'article_comment_reply_id' => 0,
                    'reid' => 0,
                    'cid' => 0,
                    'title' => '评论内容违禁处理',
                    'content' => '亲爱的用户，您在《' . $article['title'] . '》中的评论[REPLACE]由于涉嫌违禁词汇言论（' . $res['label'] . '）已做隐藏删除处理，若您有异议点击内容申诉官方人工二次审核',
                    'link' => '',
                    'target' => '',
                    'create_time' => time()
                ];
                //todo 添加消息中心
                (new MessageCenter())->pushMessage($this->user['id'], $messageData);
                return show(201, '小准准检测到您的评论涉嫌违规' . PHP_EOL . '已做处理，点击查看详情');
            }
            $rec['vip'] = (new ZlUserVip())->getUserVipLevel($this->user['id']);
            return show(config('code.success'), '评论成功', $rec);
        }
        return show(config('code.error'), '不能重复评论');
    }

    /**
     * 文章评论回复(自测ok)
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \Exception
     */
    public function reply()
    {
        $id = input('param.id/d');
        $commentid = input('param.commentid/d');
        $pid = input('param.pid/d');
        $content = htmlspecialchars(input('param.content/s', ''));
        $content = trim($content);
        if (empty($content))
        {
            return show(config('code.error'), '评论内容不能为空');
        }
        if ($this->user['no_comment'])
        {
            return show(config('code.error'), '评论失败，已被禁止评论');
        }
        //获取文章详情
        $article = (new ArticleModel())->getArticleDetails($this->user['id'], ['id' => $id]);
        if (!$article)
        {
            return show(config('code.error'), '文章不存在');
        }
        $res = AliyunFilterWord::checkText($content, 'article_comment');
        //若内容有违规且超过配置的相似比例，则进行隐藏不显示，将标签存入库中
        if ($res)
        {
            $type = 1;
            $data = [
                'comment_id' => $commentid,
                'ucid' => $this->user['uc_id'],
                'create_time' => time(),
                'check_status' => 1,
                'illegal_type' => $res['label_type'],
                'illegal_level' => $res['level'],
                'data_id' => $res['data_id'],
                'aid' => $id,
                'pid' => $pid,
                'content' => $content,
                'uid' => $this->user['id'],
                'status' => -1
            ];
        } else
        {
            $type = 2;
            $data = [
                'aid' => $id,
                'comment_id' => $commentid,
                'create_time' => time(),
                'pid' => $pid,
                'content' => $content,
                'ucid' => $this->user['uc_id'],
                'uid' => $this->user['id'],
                'status' => 0
            ];
        }

        $reply = (new ArticleCommentReply())->add_reply($this->user,$data);

        if (isset($reply['uid']) && $reply['uid'] == $this->user['id'])
        {
            Cache::rm('read_reply_' . $this->user['id']);
        }

        if ($reply)
        {
            if ($type === 1)
            {
                $messageData = [
                    'status' => 0,
                    'is_read' => 0,
                    'range' => $this->user['id'],
                    'type' => 1,
                    'article_comment_id' => 0,
                    'article_comment_reply_id' => $reply['crid'],
                    'reid' => 0,
                    'cid' => 0,
                    'title' => '评论内容违禁处理',
                    'content' => '亲爱的用户，您在《' . $article['title'] . '》中的评论[REPLACE]由于涉嫌违禁词汇言论（' . $res['label'] . '）已做隐藏删除处理，若您有异议点击内容申诉官方人工二次审核',
                    'link' => '',
                    'target' => '',
                    'create_time' => time()
                ];
                //todo 添加消息中心
                (new MessageCenter())->pushMessage($this->user['id'], $messageData);
                return show(201, '小准准检测到您的回复涉嫌违规' . PHP_EOL . '已做处理，点击查看详情');
            }
            $reply['vip'] = (new ZlUserVip())->getUserVipLevel($this->user['id']);
            return show(config('code.success'), 'Reply success . ', $reply);
        }
        return show(config('code.error'), '回复失败,请稍后再试');
    }
}
